package cn.francis.service.impl;

import cn.francis.service.SysModuleService;
import com.qfedu.agro.mapper.SysModuleMapper;
import com.qfedu.agro.pojo.SysModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Name: SysModuleServiceImpl
 * Package: cn.francis.service.impl
 * Date: 2024/10/26 - 11:38
 * Description:
 *
 * @author Junhui Zhang
 * @version v1.0
 */

@Service
public class SysModuleServiceImpl implements SysModuleService {

    @Autowired
    private SysModuleMapper sysModuleMapper;

    @Override
    public List<SysModule> findMenuByUserId(Long id) {
        List<SysModule> menu = sysModuleMapper.selectByUserId(id);
        List<SysModule> firstLevelMenus = menu.stream().filter(item -> item.getParentId() == null).toList();
        for (SysModule firstLevelMenu : firstLevelMenus) {
            List<SysModule> secondLevelMenu = menu.stream().filter(item -> item.getParentId() != null).filter(item -> item.getParentId().equals(String.valueOf(firstLevelMenu.getId()))).toList();

            firstLevelMenu.setChildren(secondLevelMenu);
        }
        return firstLevelMenus;
    }
}
